//
//  KSFeedBackPhotoCollectionViewCell.swift
//  Supermarket
//
//  Created by Black_Crystal on 2017/9/20.
//  Copyright © 2017年 kushao. All rights reserved.
//

import UIKit

protocol ChoosePhotoDelegate: NSObjectProtocol {
    func deletePhotoCallBack(row: NSInteger)
    func choosePhotCallBack()
}
 
class KSFeedBackPhotoCollectionViewCell: UICollectionViewCell {
    weak var delegate: ChoosePhotoDelegate?
    private let photoBtn = UIButton()
    private let deleteBtn = UIButton()
    private var cellRow = NSInteger()
    private let backView = UIView()
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.contentView.addSubview(backView)
        backView.snp.makeConstraints { (make) in
            make.edges.equalTo(UIEdgeInsets.init(top: 0, left: 0, bottom: 0, right: 0))
        }
        
        photoBtn.addTarget(self, action: #selector(choosePhotoEvent), for: .touchDown)
        photoBtn.isEnabled = false
        photoBtn.adjustsImageWhenDisabled = false
        backView.addSubview(photoBtn)
        photoBtn.snp.makeConstraints { (make) in
            make.edges.equalTo(UIEdgeInsetsMake(0, 0, 0, 0))
        }
        
        deleteBtn.isHidden = true
        deleteBtn.setBackgroundImage(UIImage.init(named: "Account_FeedBack_quxiao"), for: .normal)
        deleteBtn.addTarget(self, action: #selector(deletePhotoEvent), for: .touchDown)
        backView.addSubview(deleteBtn)
        deleteBtn.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(0)
            make.top.equalToSuperview().offset(0)
            make.width.height.equalTo(20)
        }
    }
    
    func setBackViewKeepLeftConstraint() {
        backView.snp.updateConstraints { (make) in
            make.edges.equalTo(UIEdgeInsets.init(top: 8, left: 8, bottom: 0, right: 5))
        }
    }
    
    func setBackViewKeepRightConstraint() {
        backView.snp.updateConstraints { (make) in
            make.edges.equalTo(UIEdgeInsets.init(top: 8, left: 5, bottom: 0, right: 8))
        }
    }
    
    @objc func deletePhotoEvent() {
        self.delegate?.deletePhotoCallBack(row: cellRow)
    }
    
    @objc func choosePhotoEvent() {
        self.delegate?.choosePhotCallBack()
    }
    
    func setBtnBackImage(image: UIImage,row: NSInteger) {
        cellRow = row
        self.deleteBtn.isHidden = false
        self.photoBtn.setBackgroundImage(image, for: .normal)
        self.photoBtn.isEnabled = false
    }
    
    func setChoosePhotoImage() {
        self.deleteBtn.isHidden = true
        self.photoBtn.setBackgroundImage(UIImage.init(named: "Account_FeedBack_AddPhoto"), for: .normal)
        self.photoBtn.isEnabled = true
    }
    
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
